﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MMITDomain.Abstract;
using MMITCore;
using MMITWeb.MMITCoreService;
using MMITWeb.Areas.Teller.Models;
using MMITDomain.Entities;
using MMITWeb.Infrastructure.Concrete;

namespace MMITWeb.Areas.Teller.Controllers
{
    [CustomAuthorize(Url = "~/Teller/UserAccount/LogOn", Roles = "Teller")]
    public class TransactionsController : Controller
    {
        //
        // GET: /Teller/Transactions/
        ITransactionRepository transactionRepository;
        ITransactionTypeRepository transactionTypeRepository;
        CoreServiceClient objClient;
        ResponseObject objResponse;

        public TransactionsController(ITransactionRepository transactionRepository, ITransactionTypeRepository transactionTypeRepository)
        {
            this.transactionRepository = transactionRepository;
            this.transactionTypeRepository = transactionTypeRepository;
            objClient = new CoreServiceClient();
            objResponse = new ResponseObject();
        }
       
        public ActionResult List(string Id)
        {
            try
            {
                if (string.IsNullOrEmpty(Id))
                {
                    //Get All requests for withdrawal Transactions...
                    var transactionType = transactionTypeRepository.GetTransactionTypeById((int)MMITCore.TransactionType.WithdrawMoney);                
                    
                    var transactions = this.transactionRepository.Transactions.Where(t => t.TransactionType.Id == transactionType.Id);
                    
                    return View(transactions);
                }
                else
                {
                    var transaction = transactionRepository.GetTransactionByCode(Id);
                    return View("View",transaction);
                }
            }
            catch(Exception Ex)
            {

            }

            return View();
        }

        public ActionResult Withdraw()
        {
            return View(new WithdrawViewModel());
        }

        [HttpPost]
        public ActionResult Withdraw(WithdrawViewModel model)
        {
            var p = transactionRepository.Transactions.Where(o => o.TransactionCode == model.Token)
                        .FirstOrDefault();
            
            ViewData["transaction"] = p;

            return View();
        }

        [HttpPost]
        public ActionResult PayCustomer(int TransactionId)
        {
            //random value to satisfy authorisation id for withdraw money...
            int tellerid = 12;

            var model = transactionRepository.GetTransactionById(TransactionId);

            decimal amount = model.Amount.HasValue? (decimal)model.Amount : 0.00M;
            
            try
            {
                objResponse = objClient.WithdrawMoney(model.TransactionCode, amount, model.RecipientId.ToString(), tellerid);

                ViewData["message"] = objResponse.Description;
                return View();
            }
            catch(Exception Ex)
            {
                ViewData["message"] = Ex.Message;
            }

            return View();
        }
    }
}
